package pers.twf.twfRpc.serializer;

import pers.twf.twfRpc.spi.SpiLoader;

/**
 * 序列化器工厂（用于获取序列化器对象）
 */
public class SerializerFactory {
    static {
        SpiLoader.load(Serializer.class);
    }

    private static volatile Serializer instance = null;

    /**
     * 默认序列化器
     */
    private static final String DEFAULT_SERIALIZERKEY = SerializerKeys.JDK;

    /**
     * 获取实例
     *
     * @param key
     * @return
     */
    public static Serializer getInstance(String key) {
        if (instance == null) {
            if (key == null) {
                instance = SpiLoader.getInstance(Serializer.class, DEFAULT_SERIALIZERKEY);
            }
            instance = SpiLoader.getInstance(Serializer.class, key);
        }
        return instance;
    }

}
